package com.bocai.freeman.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.freeman.R;
import com.bocai.freeman.view.fragments.OrderSpecialPerfFrag;
import com.jakewharton.rxbinding.view.RxView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observable;

public class OrderSpecialPerfAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public enum ITEM_TYPE {
        ITEM_TYPE_COMMON,
        ITEM_TYPE_EVALUATE,
        ITEM_TYPE_NO_PAY,
        ITEM_TYPE_PAID,
    }

    private OnItemClickLitener mOnItemClickLitener;
    private Context mContext;
    private List<Integer> mdatas;

    public OrderSpecialPerfAdapter(List<Integer> datas, Context context) {
        this.mdatas = datas;
        this.mContext = context;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position, int itemType);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE.ITEM_TYPE_COMMON.ordinal()) {

            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_common, parent, false);
            return new CommonHolder(view);

        } else if (viewType == ITEM_TYPE.ITEM_TYPE_EVALUATE.ordinal()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_evaluate, parent, false);
            return new EvaluateHolder(view);

        } else if (viewType == ITEM_TYPE.ITEM_TYPE_NO_PAY.ordinal()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_pay, parent, false);
            return new NoPayHolder(view);
        } else if (viewType == ITEM_TYPE.ITEM_TYPE_PAID.ordinal()) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_order_pay, parent, false);
            return new PaidHolder(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof CommonHolder) {
            if (mOnItemClickLitener != null) {
                click(((CommonHolder) holder).llContentCommon).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, 1);
                });
            }

        } else if (holder instanceof EvaluateHolder) {
            EvaluateHolder evaluateHolder = (EvaluateHolder) holder;
            if (mOnItemClickLitener != null) {
                click(evaluateHolder.llContentEvaluate).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, 2);
                });
                click(evaluateHolder.tvEvaluate).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, OrderSpecialPerfFrag.EVALUATE);
                });

            }
        } else if (holder instanceof NoPayHolder) {
            NoPayHolder noPayHolder = (NoPayHolder) holder;
            if (mOnItemClickLitener != null) {
                click(noPayHolder.llContactPay).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, 3);
                });

                click(noPayHolder.btnCancelOrder).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, OrderSpecialPerfFrag.CANCEL_ORDER);
                });

                click(noPayHolder.btnPayMoney).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, OrderSpecialPerfFrag.PAY_MONEY);
                });
            }
        } else if (holder instanceof PaidHolder) {
            PaidHolder paidHolder = (PaidHolder) holder;
            paidHolder.btnApplyRefund.setText(R.string.str_apply_refund);
            paidHolder.btnCommitPay.setText(R.string.str_commit_pay);
            if (mOnItemClickLitener != null) {
                click(paidHolder.llContactPay).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, 4);
                });

                click(paidHolder.btnCommitPay).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, OrderSpecialPerfFrag.COMMIT_PAY);
                });

                click(paidHolder.btnApplyRefund).subscribe(o -> {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos, OrderSpecialPerfFrag.APPLY_REFUND);
                });

            }
        }

    }


    @Override
    public int getItemViewType(int position) {

        if (mdatas.get(position) == 0) {
            return ITEM_TYPE.ITEM_TYPE_COMMON.ordinal();
        } else if (mdatas.get(position) == 1) {
            return ITEM_TYPE.ITEM_TYPE_EVALUATE.ordinal();
        } else if (mdatas.get(position) == 2) {
            return ITEM_TYPE.ITEM_TYPE_NO_PAY.ordinal();
        } else if (mdatas.get(position) == 3) {
            return ITEM_TYPE.ITEM_TYPE_PAID.ordinal();
        }
        return -1;

    }

    @Override
    public int getItemCount() {
        return mdatas.size();
    }


    public class CommonHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ll_content_common)
        LinearLayout llContentCommon;

        public CommonHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class EvaluateHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ll_content_evaluate)
        LinearLayout llContentEvaluate;
        @Bind(R.id.tv_evaluate)
        TextView tvEvaluate;

        public EvaluateHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public class NoPayHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ll_contact_pay)
        LinearLayout llContactPay;
        @Bind(R.id.btn_cancel_order)
        Button btnCancelOrder;
        @Bind(R.id.btn_pay_money)
        Button btnPayMoney;

        public NoPayHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    public class PaidHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.ll_contact_pay)
        LinearLayout llContactPay;
        @Bind(R.id.btn_cancel_order)
        Button btnApplyRefund;
        @Bind(R.id.btn_pay_money)
        Button btnCommitPay;

        public PaidHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    private <T extends Object> Observable click(View view) {
        return RxView.clicks(view)
                .throttleFirst(1000, TimeUnit.MILLISECONDS);
    }
}